package com.example.lab5_p01_layoutdemo;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button btnStart;
	Button btnOK;
	EditText txtTimeOut, txtResult;
	TextView lblTimeOut, lblSo1, lblSo2;
	int timeOut;
	boolean stopWarning = false;
	int a, b, diemSo, soCauHoi;
	Random r = new Random();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lblTimeOut = (TextView) findViewById(R.id.lblTimeOut);
		btnStart = (Button) findViewById(R.id.btnStart);
		txtTimeOut = (EditText) findViewById(R.id.txtTimeOut);
		txtResult = (EditText) findViewById(R.id.txtResult);
		btnOK = (Button) findViewById(R.id.btnOK);
		lblSo1 = (TextView) findViewById(R.id.lblSo1);
		lblSo2 = (TextView) findViewById(R.id.lblSo2);

		timeOut = Integer.parseInt(txtTimeOut.getText().toString());
		txtResult.setEnabled(false);
		btnOK.setEnabled(false);

	}

	public void startGame(View v) {
		txtResult.setEnabled(true);
		btnOK.setEnabled(true);
		txtTimeOut.setEnabled(false);
		btnStart.setEnabled(false);
		sinhCauHoi();
		
		Handler countDown = new Handler() {
			int t = timeOut;

			public void handleMessage(android.os.Message msg) {
				if(t==10){
					warning.sendEmptyMessage(0);
				}
				txtTimeOut.setText(t-- + "");
				if (t == -1) {
					stopWarning=true;
					txtResult.setEnabled(false);
					btnOK.setEnabled(false);
					lblTimeOut.setText("Ket qua ");
					txtTimeOut.setBackgroundColor(Color.RED);
					txtTimeOut.setText(diemSo + "/" + soCauHoi + "/" + timeOut);
					btnStart.setEnabled(true);
					txtResult.setText("");
					lblTimeOut.setBackgroundColor(Color.WHITE);					
					return;
				}
				this.sendEmptyMessageDelayed(0, 1000);
			}
		};
		countDown.sendEmptyMessage(0);
		txtTimeOut.setBackgroundColor(Color.WHITE);
	}

	public void pressOK(View v) {
		int result=Integer.parseInt(txtResult.getText().toString());
		if(a+b==result){
			diemSo++;
		}		
		sinhCauHoi();
		txtResult.setText("");
	}

	void sinhCauHoi() {
		a = r.nextInt(56);
		b = r.nextInt(100);
		lblSo1.setText(a + "");
		lblSo2.setText(b + "");
		soCauHoi++;
	}
	Handler warning=new Handler(){
		int i=0;
		public void handleMessage(android.os.Message msg) {
			if(stopWarning){
				return;
			}
			i++;
			if(i%2==0){
				lblTimeOut.setBackgroundColor(Color.CYAN);
			}else {
				lblTimeOut.setBackgroundColor(Color.YELLOW);
			}
			this.sendEmptyMessageDelayed(0,500);
		}		
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
